﻿using System;
using System.Reflection;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace Cybozu.SharePoint.CategoryFilter
{
    public static class Utility
    {
        public static string GetLocalizedString(string source, string resourceFile, SPWeb web)
        {
            return SPUtility.GetLocalizedString("$Resources:" + source, resourceFile, web.Language);
        }

        public static string GetServerRelativeListEditUrl(SPList splist)
        {
            string listeditUrl = SPUtility.GetServerRelativeUrlFromPrefixedUrl("~site/_layouts/listedit.aspx");
            return string.Format("{0}?List={1}", listeditUrl, SPHttpUtility.UrlKeyValueEncode(splist.ID));
        }

        public static object GetFieldAttribute(SPField field, string name)
        {
            Type baseType = field.GetType();
            MethodInfo mi = baseType.GetMethod("GetFieldAttributeValue", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[] { typeof(String) }, null);
            return mi.Invoke(field, new object[] { name });
        }

        public static void SetFieldAttribute(SPField field, string name, string value)
        {
            Type baseType = field.GetType();
            MethodInfo mi = baseType.GetMethod("SetFieldAttributeValue", BindingFlags.Instance | BindingFlags.NonPublic);
            mi.Invoke(field, new object[] { name, value });
        }
    }
}
